package com.example.list_view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

/**
 * 数据适配器
 */
public class Data_Adaopter extends ArrayAdapter<Entity> {   //泛型为实体类类型
    private int resource_id;

    public Data_Adaopter(@NonNull Context context, int textViewResourceId, @NonNull List<Entity> objects) {
        super(context, textViewResourceId, objects);
        resource_id = textViewResourceId; //找模型资源id
    }

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Entity entity=getItem(position); //获取当前实体类的资源
        View view =LayoutInflater.from(getContext()).inflate(resource_id,parent,false);
        ImageView img=view.findViewById(R.id.img);  //找到模型图片
        TextView title=view.findViewById(R.id.title);   //找到模型标题
        img.setImageResource(entity.getImg());  //设置实体数据图片
        title.setText(entity.getTitle());       //设置实体数据标题
        return view;
    }
}